<template>
  <export-excel
    ref="excelObj"
    :data="exData"
    :fields="exFields"
    :name="fileName"
    :is-click-generate="isClickGenerate"
    :before-generate="beforeStart"
    :before-finish="beforeFinish"
    :is-marlt="isMarlt"
    :title="contentTitle"
    :transform-time-key="transformTimeKey"
  >
    <slot name="excelBtn" />
  </export-excel>
</template>
<script>
    import exportExcel from '@/components/JsonExcel/JsonExcel'

    export default {
        name: 'Excel',
        components: {
            'exportExcel': exportExcel
        },
        props: {
            fileName: {
                type: String,
                default: 'fileName'
            },
            //开始上传之前操作
            beforeStart: {
                type: Function
            },
            //上传完成之前操作
            beforeFinish: {
                type: Function
            },
            //是否点击导出excel
            isClickGenerate: {
                type: String,
                default: "true"
            },
            isMarlt: {
                type: Boolean,
                default: true
            },
            // 内容的标题，String Array<String>
            contentTitle: {
                type: [String, Array],
                default: null
            },
          transformTimeKey: {
            type: Array,
            default() {
              return []
            }
          },
        },
        data: function () {
            return {
                exData: [],
                exFields: {}
            }
        },
        mounted: function () {
        },
        methods: {
            /**
             * 设置表格内容并手动生成excel文件并下载
             * exData 文件内容（JSON格式） tableObj 表格对象，startIndex 表格导出开始位置，默认为1
             */
            setDataAndGenerate: function (exData) {
              if (!exData) {
                    return false;
                }
                this.setExcelData(exData);
                var _this = this;
                setTimeout(function () {
                    _this.generate();
                }, 50);
            },
            /**
             * 手动生成excel文件并下载
             * exData 文件内容（JSON格式） tableObj 表格对象，startIndex 表格导出开始位置，默认为1
             */
            generateExcel: function (exData, tableObj, startIndex) {
                if (!exData || !tableObj) {
                    return false;
                }
                this.tableToExcelFile(tableObj, startIndex);
                this.setExcelData(exData);
                var _this = this;
                setTimeout(function () {
                    _this.generate();
                }, 50);
            },
            /**
             * 获取table的引用，根据定义的table转换成excel导出需要的字段格式
             * tableObj 表格对象，startIndex 表格导出开始位置，默认为2
             */
            tableToExcelFile: function (tableObj, startIndex) {
                if (!tableObj) {
                    return null;
                }
                if (!startIndex && startIndex != 0) {
                    startIndex = 1;
                }
                var columns = tableObj.columns;
                if (!columns || columns.length <= startIndex) {
                    return null;
                }
                var excelFileData = {};
                for (var i = startIndex; i < columns.length; i++) {
                    var column = columns[i];
                    if (column.title && column.dataIndex && column.dataIndex != '' && column.dataIndex != null) {
                        if(column.dataIndex !== "operation" && column.dataIndex !== "actions"){
                            excelFileData[column.title] = column.dataIndex;
                        }
                    }
                }
                this.setExcelFields(excelFileData);
            },
            setExcelFields: function (exFields) {
                this.exFields = exFields;
                // console.log(this.exFields);
            },
            getExcelFields: function () {
                return this.exFields;
            },
            setExcelData: function (exData) {
                this.exData = exData;
            },
            getExcelData: function () {
                return this.exData;
            },
            generate: function () {
                if (this.exData && this.exFields) {
                    this.$refs.excelObj.generate();
                }
            },
            initFild: function (obj) {
                var newObj = {};
                if (obj instanceof Array) {
                    newObj = [];
                }
                for (var key in obj) {
                    var val = obj[key];
                    if (val == null || typeof(val) == 'undefined' || (typeof(val) != 'object' && val.length <= 0)) {
                        //把null值替换为value
                        newObj[key] = "-";
                    } else {
                        newObj[key] = typeof val === 'object' ? this.initFild(val) : val;
                    }
                }
                return newObj;
            },
        }
    }
</script>
